package com.easyadmin.pro.tool;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Objects;

/**
 * packageName com.easyadmin.pro.tool
 *
 * @author 骑着蚂蚁去上天
 * @version JDK 17
 * @className ExceptionUtils
 * @date 2024/4/7
 * @description 异常信息处理工具
 */
@Slf4j
public class ExceptionUtils {

    /**
     * 将异常日志转换为字符串
     * @param t
     * @return String
     */
    public static String conventStringException(Throwable t) {
        Writer writer = null;
        PrintWriter printWriter = null;
        try {
            writer = new StringWriter();
            printWriter = new PrintWriter(writer);
            t.printStackTrace(printWriter);
            return writer.toString();
        } finally {
            try {
                if (Objects.nonNull(writer)) {
                    writer.close();
                }
                if (Objects.nonNull(printWriter)) {
                    printWriter.close();
                }
            } catch (IOException e) {
                log.error("conversion string io exception：" + conventStringException(e));
            }
        }
    }
}